<template>
  <div class="search-result">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.art_id" :title="item.title">
        <div slot="label">
          {{ item.aut_name }}&nbsp;丨&nbsp;{{ item.pubdate }}
        </div>
      </van-cell>
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from "@/api/search";

export default {
  name: "searchResult",
  props: {
    searchText: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      // 页码
      page: 1,
      // 每页大小
      perPage: 10,
    };
  },
  computed: {},
  methods: {
    async onLoad() {
      // 1.请求获取数据
      const { data } = await getSearchResult({
        page: this.page, // 页码
        per_page: this.perPage, // 每页大小
        q: this.searchText, // 搜索的字符
      });
      // 2.将数据放到数据列表中
      const { results } = data.data;
      this.list.push(...results);

      // 3.关闭本次的 loading
      this.loading = false;

      // 4.判断是否还有数据 有获取下一页；没有不能再加载
      if (results.length) {
        this.page++;
      } else {
        this.finished = true;
      }
    },
  },
};
</script>
<style lang='less' scoped>
.search-result {
  position: fixed;
  top: 112px;
  left: 0;
  right: 0;
  bottom: 0;
  overflow-y: auto;
}
</style>